package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public Member add(Member member) {
        memberDao.add(member);
        //这里是可以拿到主键的
        return member;//返回之后就可以在web拿到主键
    }

    @Override
    public List<Member> getFamily(Integer id) {
        List<Member> members = memberDao.getFamily(id);
        return members;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用分页插件，pageHelper实现分页，底层是mybatis拦截器原理
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据用户输入的查询条件进行查询，跟分页无关
        List<Member> members = memberDao.findAll(queryPageBean.getQueryString());
        //返回总记录数和分页数据
        return new PageResult(page.getTotal(), members);
    }
}
